

* +++++++++++++++++++++
* CLEAN EU ELECTION DATA
* +++++++++++++++++++++


* election results European elections 2019
* +++++++++++++++

import delimited "${data_raw}/elections/european_elections2019.csv", clear

* focus on Kreis level results
keep if _n>=777

* focus on main parties
keep if v9 == "CDU" | v9 == "CSU" | v9 == "SPD" | v9 == "GRÃNE" ///
	| v9 == "AfD" | v9 == "FDP" | v9 == "DIE LINKE" | v9 == "WÃ¤hler/-innen" | v9 =="NPD"

keep v4 v9 v13
replace v9 = "frac_voted" if v9 == "WÃ¤hler/-innen"
replace v9 = "green" if v9 == "GRÃNE"
replace v9 = "left" if v9 == "DIE LINKE"
replace v9 = lower(v9)	
replace v13 = regexr(v13, ",", ".")
destring v13, replace force

* reshape wide on party
reshape wide v13, i(v4) j(v9) string
ren v13* *

* combine cdu and csu 
replace cdu = csu if cdu ==. & csu !=.
replace csu = cdu if csu ==. & cdu !=.
drop csu
ren cdu cdu_csu

* combine afd and npd 
gen afd_npd = afd + npd
 
ren v4 ags
destring ags, replace
ds ags, not
renvars `r(varlist)', suff(_2019)

tempfile election_results2019 
save `election_results2019'


* election results European elections 2014
* +++++++++++++++

import delimited "${data_raw}/elections/ew14_kerg.csv", clear delimiter(";")

* focus on Kreis level results
destring v3, replace force 
drop if v3 == . | v3 == 99 

* focus on main variables and parties
ren v1 ags
ren v2 place_name
ren v4 eligible_to_vote
ren v6 votes
ren v12 cdu 
ren v14 spd 
ren v16 green
ren v18 fdp 
ren v20 left
ren v22 csu 
ren v52 afd 
ren v58 npd

* combine Osterode am Harz and Göttingen because today it is just one Kreis
destring ags eligible_to_vote votes cdu spd green fdp left csu afd npd, replace
replace ags = 3159 if inlist(ags, 3152, 3156)
replace place_name ="Göttingen" if inlist(ags, 3152, 3156)
collapse (rawsum) eligible_to_vote votes cdu spd green fdp left csu afd npd ///
	(first) place_name, by(ags)

* calculate vote shares
foreach party in cdu spd green fdp left csu afd npd {
	destring `party', replace
	gen frac_`party' = `party' / votes
	drop `party'
	ren frac_`party' `party'
}

* fraction of people who voted
gen frac_voted = votes / eligible_to_vote

* combine cdu and csu 
replace cdu = csu if cdu ==. & csu !=.
replace csu = cdu if csu ==. & cdu !=.
drop csu
ren cdu cdu_csu

* combine afd and npd 
gen afd_npd = afd + npd

ds ags place_name, not
renvars `r(varlist)', suff(_2014)

* combine with 2019 and save
merge 1:1 ags using `election_results2019', assert(3) nogen
save "${data_derived}/european_election_results", replace
